﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="wLogin.xaml.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Interaction logic for wLogin.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

// ReSharper disable InconsistentNaming
namespace Mar3ek.HouseLight
{
    using System.Windows;

    /// <summary>
    /// Interaction logic for wLogin.xaml
    /// </summary>
    public partial class wLogin
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="wLogin"/> class.
        /// </summary>
        public wLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Checked event of the rbToken control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void rbToken_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded)
            {
                return;
            }

            txtUsername.IsEnabled = false;
            pbPassword.IsEnabled = false;
            txtToken.IsEnabled = true;
        }

        /// <summary>
        /// Handles the Checked event of the rbBasic control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void rbBasic_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded)
            {
                return;
            }

            txtUsername.IsEnabled = true;
            pbPassword.IsEnabled = true;
            txtToken.IsEnabled = false;
        }

        /// <summary>
        /// Handles the Loaded event of the Window control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtUsername.Focus();
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        /// <summary>
        /// Handles the Click event of the btnLogin control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtAccount.Text))
            {
                return;
            }

            Properties.Settings.Default.Account = txtAccount.Text;
            if (rbBasic.IsChecked == true && !string.IsNullOrEmpty(txtUsername.Text) && !string.IsNullOrEmpty(pbPassword.Password))
            {
                Properties.Settings.Default.Username = txtUsername.Text;
                Properties.Settings.Default.Password = pbPassword.Password;
                Properties.Settings.Default.Token = string.Empty;
                Properties.Settings.Default.Save();
                DialogResult = true;
                Close();
            }
            else if (rbToken.IsChecked == true && !string.IsNullOrEmpty(txtToken.Text))
            {
                Properties.Settings.Default.Username = string.Empty;
                Properties.Settings.Default.Password = string.Empty;
                Properties.Settings.Default.Token = txtToken.Text;
                Properties.Settings.Default.Save();
                DialogResult = true;
                Close();
            }
        }
    }
}

// ReSharper restore InconsistentNaming